package com.example.employee.restfulapi.controller;


import com.example.employee.restfulapi.entity.Company;
import com.example.employee.restfulapi.mapper.CompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/company")
public class CompanyController {
    //在此处完成Company API
    @Autowired
    CompanyMapper companyMapper;


    @GetMapping("/GET/companies")
    public List<Company> getAllCompanies(){
        return companyMapper.selectList(null);
    }
    @GetMapping("/GET/companies/id")
    public Company getCompany(long id){
        return companyMapper.selectById(id);
    }


    @PostMapping("/POST/companies")
    public String addCompany(@RequestBody Company company){
        int result = companyMapper.insert(company);
        if (result > 0) {
            return "ok";
        } else {
            return "err";
        }
    }
    @PutMapping("/PUT/companies/id")
    public String updateCompany(@RequestBody Company company, int id){
        company.setId((long) id);
        int result = companyMapper.updateById(company);
        if(result>0){
            return "ok";
        }else {
            return "err";
        }
    }
    @DeleteMapping("/DEL/companies/id")
    public String delCompany(int id){

            int result = companyMapper.deleteById(id);
            if(result>0){
                return "ok";

            }else {
                return "err";
            }


    }
}
